package com.ysepay.utils;

import com.ysepay.config.PropertyConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

/**
 * Created by zhongchunyan
 *
 * @Date 2017/9/13 17:25
 */
@Component
public class YSSubmitUtils {

    @Resource
    private PropertyConfig config;
    private static YSSubmitUtils util;
    @PostConstruct
    public void init(){
        util = this;
    }
    /**
     * API验证签名工具，把签名值，请求字符编码，返回结果(json body)传递过来进行验证签名 公钥验证签名
     * 用于验证银盛同步响应回来的参数
     * @param request
     * @param sign
     * @param responseBody
     * @param charset
     * @return
     * @see
     */
    public static boolean verifyJsonSign(HttpServletRequest request, String sign, String responseBody, String charset) {
        ServletContext servletContext = request.getServletContext();
        InputStream publicCertFileInputStream = servletContext
                .getResourceAsStream(util.config.getPATH_YSEPAY_PUBLIC_CERT());
        boolean isSign = false;
        try {
            isSign = SignUtils.rsaCheckContent(publicCertFileInputStream, responseBody, sign, charset);
        } catch (Exception e) {
            throw new RuntimeException("验证签名失败，请检查银盛公钥证书文件是否存在");
        }
        return isSign;
    }
}
